<?php
/**
 * Created by PhpStorm.
 * User: lih
 * Date: 2018/4/22
 * Time: 20:09
 */

namespace frontend\api\action;

use Yii;
use yii\db\Query;
use common\models\User;

class Addpost extends BaseAction
{

    /**
     * @param $arrayData array
     * @param $returnData array
     * @throws \Exception
     */
    public function runAction($arrayData, &$returnData)
    {

        $user = Yii::$app->user;

        if($user->isGuest) {
            $returnData['msg'] = '用户未登录';
            return ;
        }

        $paramFid = $this->trimNullParam(@$arrayData['fid']);
        $postDataArray = $arrayData['data'];
        $paramSubject = $this->trimNullParam(@$postDataArray['subject']);
        $mediaextraStr = '';
        $mediaextraArray = @$postDataArray['mediaextra'];
        if(!empty($mediaextraArray)) {
            $mediaextraStr = json_encode($mediaextraArray);
        }

        $createtime = time();
        $tableNameThread = $this->getDzTableName('forum_thread');
        $tableNamePost = $this->getDzTableName('forum_post');
        $tableForumPostTableid = $this->getDzTableName('forum_post_tableid');
        $tableForumForum = $this->getDzTableName('forum_forum');

        try {
            /**
             * @var $userModel \common\models\User
             */
            $userModel = User::findIdentity($user->getId());
            if($userModel !== null) {

                $dbConn = Yii::$app->db;

                $authorName = $userModel->username;

                //insert into forum_thread
                $dbConn->createCommand()
                    ->insert($tableNameThread, [
                        'fid' => $paramFid,
                        'author' => $authorName,
                        'authorid' => $userModel->getId(),
                        'subject' => $paramSubject,
                        'mediaextra' => $mediaextraStr,
                        'dateline' => $createtime,
                        'lastpost' => $createtime,
                    ])
                    ->execute();

                $insertTid = $dbConn->getLastInsertID();

                //insert into forum_post_tableid
                $dbConn->createCommand()
                    ->insert($tableForumPostTableid, [
                        'pid' => '',
                    ])
                    ->execute();

                $insertPid = $dbConn->getLastInsertID();

                //insert into forum_post
                $dbConn->createCommand()
                    ->insert($tableNamePost, [
                        'pid' => $insertPid,
                        'tid' => $insertTid,
                        'fid' => $paramFid,
                        'first' => '1',
                        'author' => $authorName,
                        'authorid' => $userModel->getId(),
                        'subject' => $paramSubject,
                        'message' => '',
                        'dateline' => $createtime,
                    ])
                    ->execute();

                /**
                // update forum_forum
                $dbConn->createCommand()
                    ->update($tableForumForum, [
                            'threads' => new yii\db\Expression('`threads` + 1'),
                            'posts' => new yii\db\Expression('`posts` + 1'),
                        ],
                        'fid=:fid', [
                            ':fid' => $paramFid,
                        ])
                    ->execute();
                 */

                $returnData['code'] = '0';
                $returnData['msg'] = '';

            }
        } catch(\Exception $e) {
            Yii::error('addpost exception: ' . $e->getMessage());
        }

    }

}
